﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Practices.Unity;

namespace MassPokerTool.Infrastructure.Threading
{
    public sealed class DispatcherService : IDispatcherService
    {
        private readonly Dispatcher _dispatcher;

        public bool IsSynchronized
        {
            get { return _dispatcher.Thread == Thread.CurrentThread; }
        }

        [InjectionConstructor]
        public DispatcherService() : this(Dispatcher.CurrentDispatcher)
        {
        }

        public DispatcherService(Dispatcher dispatcher)
        {
            Debug.Assert(dispatcher != null);
            _dispatcher = dispatcher;
        }

        public void Invoke(Action action)
        {
            Debug.Assert(action != null);
            _dispatcher.Invoke(action);
        }

        public void BeginInvoke(Action action)
        {
            Debug.Assert(action != null);
            _dispatcher.BeginInvoke(action);
        }

        public void BeginInvoke(Action action, DispatcherPriority priority)
        {
            Debug.Assert(action != null);
            _dispatcher.BeginInvoke(action, priority);
        }
    }
}
